<template>
  <div class="shopcart">
    <div class="content">
      <!-- 右侧定宽，左侧自适应 -->
      <div class="content-left">
        <div class="logo-wrapper">
          <div class="logo" :class="{'highlight': totalCount>0}">
            <i class="icon-shopping_cart" :class="{'highlight': totalCount>0}"></i>
          </div>
          <div class="num" v-show="totalCount>0">{{totalCount}}</div>
        </div>
        <div class="price" :class="{'highlight': totalPrice>0}">¥{{totalPrice}}</div>
        <div class="desc">另需配送费¥{{deliveryPrice}}</div>
      </div>
      <div class="content-right">
        <div class="pay" :class="payClass">¥{{minPrice}}起送</div>
      </div>
    </div>
    <!-- 抛物线效果:通过show控制小球的显隐; 内层控制旋转 -->
    <div class="ball-container">
      <div class="" transition="drop" v-for="ball in balls" v-show="ball.show">
        <div class="inner inner-hook"></div>
      </div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
  export default{
    data () {
      return {
        bolls: [{
          show: false
        }, {
          show: false
        }, {
          show: false
        }, {
          show: false
        }, {
          show: false
        }],
        dropBalls: []
      };
    },
    // 接收从goods组件传递的参数
    props: {
      // 选中的food
      selectedFoods: {
        type: Array,
        // 当类型为array或object，default是一个函数
        default() {
          return [];
        }
      },
      deliveryPrice: {
        type: Number,
        default: 0
      },
      minPrice: {
        type: Number,
        default: 0
      }
    },
    method: {
      drop (el) {
        // 需要知道当前点击的位置(相对于当前视口)
        for (let i = 0; i < this.balls.length; i++) {
          let ball = this.balls[i];
          if (!ball.show) {
            ball.show = true;
            ball.el = el;
            this.dropBalls.push(ball);
            return;
          }
        }
      },
      transitions: {
        drop: {
          beforeEnter(el) {
            let count = this.balls.length;
            while (count--) {
              let ball = this.balls[count];
              if (ball.show) {
                // 获取当前元素相对于视口的位置
                let rect = ball.el.getBoundingClientRect();
                let x = rect.left - 32;
                let y = -(window.innerHeight - rect.top - 22);
                // 小球初始位置
                el.style.display = '';
                el.style.webkitTransform = `translate3d(0, ${y}px, 0)`;
                el.style.transform = `translate3d(0, ${y}px, 0)`;
                let inner = el.getElementsByClassName('inner-hook')[0];
                inner.style.transform = `translate3d(${x}px, 0,0)`;
              }
            }
          },
          enter(el) {

          },
          afterEnter(el) {

          }
        }
      }
    },
    computed: {
      totalPrice () {
        let total = 0;
        this.selectedFoods.forEach((food) => {
          total += food.price * food.count;
        });
        return total;
      },
      totalCount () {
        let count = 0;
        this.selectedFoods.forEach((food) => {
          count += food.count;
        });
        return count;
      },
      payDesc () {
        if (this.totalPrice === 0) {
          return `¥${this.minPrice}元起送`;
        } else if (this.totalPrice < this.minPrice) {
          let diff = this.totalPrice - this.minPrice;
          return `还差¥${diff}元起送`;
        } else {
          return '去结算';
        }
      },
      payClass () {
        if (this.totalPrice < this.minPrice) {
          return 'not-enough';
        } else {
          return 'enough';
        }
      }
    }
  };
</script>

<style lang="stylus" rel="stylesheet/stylus">
  .shopcart
    width: 100%
    height: 48px
    position: fixed
    left: 0px
    bottom: 0px
    z-index: 50
    .content
      display: flex
      background: #141d27
      color: rgba(255,255,255,0.4)
      /* 左侧自适应  */
      .content-left
        flex: 1
        font-size: 0
        .logo-wrapper
          display: inline-block
          vertical-align: top
          width: 56px
          height: 56px
          position: relative
          top: -10px
          margin: 0 12px
          padding: 6px
          border-radius: 50%
          background: #141d27
          box-sizing: border-box
          .logo
            width: 100%
            height: 100%
            border-radius: 50%
            background: #2b343c
            text-align: center
            &.highlight
              background: rgb(0,160,220)
            .icon-shopping_cart
              font-size: 24px
              color: #80858a
              &.highlight
                color: #fff
          .num
            position: absolute
            top: 0px
            right: 0px
            width: 24px
            height: 16px
            line-height: 16px
            text-align: center
            border-radius: 16px
            font-size: 9px
            color: #fff
            font-weight: 700
            background: rgb(240,20,20)
            /* 投影方式(默认外阴影) X轴偏移量(正右负左) Y轴偏移量(正下负上) 阴影模糊半径 阴影扩展半径 阴影颜色;*/
            box-shadow: 0 4px 8px 0 rgba(0,0,0,0.4)
        .price
          display: inline-block
          margin-top: 12px
          padding-right: 12px
          vertical-align: top
          line-height: 24px
          font-size: 16px
          font-weight: 700
          border-right: 1px solid #rgba(255,255,255,0.1)
          box-sizing: border-box
          &.highlight
            color: #fff
        .desc
          display: inline-block
          margin: 12px 0 0 12px
          vertical-align: top
          line-height: 24px
          font-size: 10px
      /* 右侧定宽 */
      .content-right
        flex: 0 0 105px
        width: 105px
        .pay
          height: 48px
          line-height: 48px
          text-align: center
          font-size: 12px
          font-weight: 700
          background: #2b333b
          &.not-enough
            background: #2b333b
          &.enough
            background: #fff
    .ball-container
      .ball
        position: fixed
        left: 32px
        bottom: 22px
        z-index: 200
        &.drop-transition
          transition: all 0.4s
          .inner
            width: 16px
            height: 16px
            border-radius: 50%
            background: rgb(0,160,220)
            transition: all 0.4s
</style>
